package com.skt.common_hr.overtime.model
{
	import com.skt.common_hr.common.Variables;
	import com.skt.common_hr.common.puremvc.CommonProxy;
	import com.skt.common_hr.overtime.model.vo.MonthlyDeptVO;
	
	import mx.collections.ArrayCollection;
	import mx.formatters.DateFormatter;
	import mx.rpc.events.FaultEvent;
	import mx.rpc.events.ResultEvent;

	public class MonthlyDeptListProxy extends CommonProxy
	{
		public static const NAME:String = "MonthlyDeptListProxy";
		public static const LOAD_MONTHLY_DEPT_LIST_COMPLETE:String = "loadMonthlyDeptListComplete";
		public static const LOAD_MONTHLY_DEPT_LIST_FAILED:String = "loadMonthlyDeptListFailed";
		public static const LOAD_MONTHLY_DEPT_INFO_COMPLETE:String = "loadMonthlyDeptInfoComplete";
		public static const LOAD_MONTHLY_DEPT_INFO_FAILED:String = "loadMonthlyDeptInfoFailed";
		
		
		public var list:ArrayCollection;
		public var listOrgeh:ArrayCollection;
		
		public var ORGEH:String;
		public var ORGTX:String;
		public var permit:String;
		public var e_doublae:String; // 겸직 체크 
		public var bReadOnly:Boolean = false;
		
		public var deptInfo:MonthlyDeptVO = null;
		
		
		public function MonthlyDeptListProxy(data:Object=null)
		{
			super(NAME, data);
		}
		
		// 목록
		public function load(orgCode:String, month:String):void
		{
			
			var params:Object = new Object();
			if(month=="") {
				var df:DateFormatter = new DateFormatter();
				df.formatString = "YYYYMM";
				month = df.format(new Date());
			}
			params.I_MONTHS = month;
			params.I_PERNR = Variables.myPernr;
			params.I_ORGEH = orgCode;
			
			loadData("Z_HR_0113", params, listResult, listFault);
		}
		
		
		private var orgCode:String;
		// 목록
		public function loadInfo(orgCode:String, month:String):void
		{
			
			var params:Object = new Object();
			if(month=="") {
				var df:DateFormatter = new DateFormatter();
				df.formatString = "YYYYMM";
				month = df.format(new Date());
			}
			params.I_MONTHS = month;
			params.I_PERNR = Variables.myPernr;
			params.I_ORGEH = orgCode;
			this.orgCode  = orgCode;
			
			loadData("Z_HR_0113", params, infoResult, infoFault);
		}
		

		public function getOT(d:Date, dept:String):Number
		{
			for(var i:int=0; i<list.length; i++) {
				//list.getItemAt(i)["DAY"
			}
			
			return 0;
		}
		
		
		///////////////////////////////////////////////////////////////////////////////////////////////////
		//
		//	Event Listeners
		//
		///////////////////////////////////////////////////////////////////////////////////////////////////
		/**
		 * 서버에서 정상적으로 리턴된 결과를 처리
		 * 
		 * @param
		 * @return void
		 */
		private function listResult(event:ResultEvent):void
		{
			if(retCode==0) {    
				list = parseList(event.result.T_OUT);				
				 
				if(Variables.company=="SAC") {
					listOrgeh = event.result.T_OUT1;
					e_doublae= event.result.E_DOUBLAE;					
				}

								  
				ORGEH = event.result.E_ORGEH;
				ORGTX = event.result.E_ORGTX;
				permit= event.result.E_JIKCHAK;

				
				bReadOnly = (event.result.E_READONLY != "");
				facade.sendNotification(LOAD_MONTHLY_DEPT_LIST_COMPLETE, this);
			}
			else {
				facade.sendNotification(LOAD_MONTHLY_DEPT_LIST_FAILED, this);
			}
		}
		
		/**
		 * 서버에서 오류가 발생했을 처리
		 * 
		 * @param
		 * @return void
		 */
		private function listFault(event:FaultEvent):void
		{
			facade.sendNotification(LOAD_MONTHLY_DEPT_LIST_FAILED, this);
		}
	

		/**
		 * 서버에서 정상적으로 리턴된 결과를 처리
		 * 
		 * @param
		 * @return void
		 */
		private function infoResult(event:ResultEvent):void
		{
			if(retCode==0) {  
				deptInfo = parseList2(event.result.T_OUT);
				facade.sendNotification(LOAD_MONTHLY_DEPT_INFO_COMPLETE, this);
			}
			else {
				deptInfo =null;
				facade.sendNotification(LOAD_MONTHLY_DEPT_INFO_FAILED, this);
			}
		}
		
		/**
		 * 서버에서 오류가 발생했을 처리
		 * 
		 * @param
		 * @return void
		 */
		private function infoFault(event:FaultEvent):void
		{
			deptInfo = null;
			facade.sendNotification(LOAD_MONTHLY_DEPT_INFO_FAILED, this);
		}


		private function parseList(src:ArrayCollection):ArrayCollection
		{
			var list:ArrayCollection = new ArrayCollection;
			for each(var rec:Object in src) {
				var vo:MonthlyDeptVO = new MonthlyDeptVO;
				vo.ORGEH = rec.ORGEH;
				vo.ORGTX = rec.ORGTX;
				vo.ZYEAR = rec.ZYEAR;
				vo.ZMONTH = rec.ZMONTH;
				vo.sum = 0;
				vo.DAY01 = (rec.DAY01==0) ? "" : Number(rec.DAY01).toString();	vo.sum += Number(rec.DAY01);
				vo.DAY02 = (rec.DAY02==0) ? "" : Number(rec.DAY02).toString();	vo.sum += Number(rec.DAY02);
				vo.DAY03 = (rec.DAY03==0) ? "" : Number(rec.DAY03).toString();	vo.sum += Number(rec.DAY03);
				vo.DAY04 = (rec.DAY04==0) ? "" : Number(rec.DAY04).toString();	vo.sum += Number(rec.DAY04);
				vo.DAY05 = (rec.DAY05==0) ? "" : Number(rec.DAY05).toString();	vo.sum += Number(rec.DAY05);
				vo.DAY06 = (rec.DAY06==0) ? "" : Number(rec.DAY06).toString();	vo.sum += Number(rec.DAY06);
				vo.DAY07 = (rec.DAY07==0) ? "" : Number(rec.DAY07).toString();	vo.sum += Number(rec.DAY07);
				vo.DAY08 = (rec.DAY08==0) ? "" : Number(rec.DAY08).toString();	vo.sum += Number(rec.DAY08);
				vo.DAY09 = (rec.DAY09==0) ? "" : Number(rec.DAY09).toString();	vo.sum += Number(rec.DAY09);
				vo.DAY10 = (rec.DAY10==0) ? "" : Number(rec.DAY10).toString();	vo.sum += Number(rec.DAY10);
				vo.DAY11 = (rec.DAY11==0) ? "" : Number(rec.DAY11).toString();	vo.sum += Number(rec.DAY11);
				vo.DAY12 = (rec.DAY12==0) ? "" : Number(rec.DAY12).toString();	vo.sum += Number(rec.DAY12);
				vo.DAY13 = (rec.DAY13==0) ? "" : Number(rec.DAY13).toString();	vo.sum += Number(rec.DAY13);
				vo.DAY14 = (rec.DAY14==0) ? "" : Number(rec.DAY14).toString();	vo.sum += Number(rec.DAY14);
				vo.DAY15 = (rec.DAY15==0) ? "" : Number(rec.DAY15).toString();	vo.sum += Number(rec.DAY15);
				vo.DAY16 = (rec.DAY16==0) ? "" : Number(rec.DAY16).toString();	vo.sum += Number(rec.DAY16);
				vo.DAY17 = (rec.DAY17==0) ? "" : Number(rec.DAY17).toString();	vo.sum += Number(rec.DAY17);
				vo.DAY18 = (rec.DAY18==0) ? "" : Number(rec.DAY18).toString();	vo.sum += Number(rec.DAY18);
				vo.DAY19 = (rec.DAY19==0) ? "" : Number(rec.DAY19).toString();	vo.sum += Number(rec.DAY19);
				vo.DAY20 = (rec.DAY20==0) ? "" : Number(rec.DAY20).toString();	vo.sum += Number(rec.DAY20);
				vo.DAY21 = (rec.DAY21==0) ? "" : Number(rec.DAY21).toString();	vo.sum += Number(rec.DAY21);
				vo.DAY22 = (rec.DAY22==0) ? "" : Number(rec.DAY22).toString();	vo.sum += Number(rec.DAY22);
				vo.DAY23 = (rec.DAY23==0) ? "" : Number(rec.DAY23).toString();	vo.sum += Number(rec.DAY23);
				vo.DAY24 = (rec.DAY24==0) ? "" : Number(rec.DAY24).toString();	vo.sum += Number(rec.DAY24);
				vo.DAY25 = (rec.DAY25==0) ? "" : Number(rec.DAY25).toString();	vo.sum += Number(rec.DAY25);
				vo.DAY26 = (rec.DAY26==0) ? "" : Number(rec.DAY26).toString();	vo.sum += Number(rec.DAY26);
				vo.DAY27 = (rec.DAY27==0) ? "" : Number(rec.DAY27).toString();	vo.sum += Number(rec.DAY27);
				vo.DAY28 = (rec.DAY28==0) ? "" : Number(rec.DAY28).toString();	vo.sum += Number(rec.DAY28);
				vo.DAY29 = (rec.DAY29==0) ? "" : Number(rec.DAY29).toString();	vo.sum += Number(rec.DAY29);
				vo.DAY30 = (rec.DAY30==0) ? "" : Number(rec.DAY30).toString();	vo.sum += Number(rec.DAY30);
				vo.DAY31 = (rec.DAY31==0) ? "" : Number(rec.DAY31).toString();	vo.sum += Number(rec.DAY31);
				list.addItem(vo);
			}
			return list;
		}	
		
		
		private function parseList2(src:ArrayCollection):MonthlyDeptVO
		{
			var list:ArrayCollection = new ArrayCollection;
			for each(var rec:Object in src) {
				if(rec.ORGEH == orgCode) {
					var vo:MonthlyDeptVO = new MonthlyDeptVO;
					vo.ORGEH = rec.ORGEH;
					vo.ORGTX = rec.ORGTX;
					vo.ZYEAR = rec.ZYEAR;
					vo.ZMONTH = rec.ZMONTH;
					vo.sum = 0;
					vo.DAY01 = (rec.DAY01==0) ? "" : Number(rec.DAY01).toString();	vo.sum += Number(rec.DAY01);
					vo.DAY02 = (rec.DAY02==0) ? "" : Number(rec.DAY02).toString();	vo.sum += Number(rec.DAY02);
					vo.DAY03 = (rec.DAY03==0) ? "" : Number(rec.DAY03).toString();	vo.sum += Number(rec.DAY03);
					vo.DAY04 = (rec.DAY04==0) ? "" : Number(rec.DAY04).toString();	vo.sum += Number(rec.DAY04);
					vo.DAY05 = (rec.DAY05==0) ? "" : Number(rec.DAY05).toString();	vo.sum += Number(rec.DAY05);
					vo.DAY06 = (rec.DAY06==0) ? "" : Number(rec.DAY06).toString();	vo.sum += Number(rec.DAY06);
					vo.DAY07 = (rec.DAY07==0) ? "" : Number(rec.DAY07).toString();	vo.sum += Number(rec.DAY07);
					vo.DAY08 = (rec.DAY08==0) ? "" : Number(rec.DAY08).toString();	vo.sum += Number(rec.DAY08);
					vo.DAY09 = (rec.DAY09==0) ? "" : Number(rec.DAY09).toString();	vo.sum += Number(rec.DAY09);
					vo.DAY10 = (rec.DAY10==0) ? "" : Number(rec.DAY10).toString();	vo.sum += Number(rec.DAY10);
					vo.DAY11 = (rec.DAY11==0) ? "" : Number(rec.DAY11).toString();	vo.sum += Number(rec.DAY11);
					vo.DAY12 = (rec.DAY12==0) ? "" : Number(rec.DAY12).toString();	vo.sum += Number(rec.DAY12);
					vo.DAY13 = (rec.DAY13==0) ? "" : Number(rec.DAY13).toString();	vo.sum += Number(rec.DAY13);
					vo.DAY14 = (rec.DAY14==0) ? "" : Number(rec.DAY14).toString();	vo.sum += Number(rec.DAY14);
					vo.DAY15 = (rec.DAY15==0) ? "" : Number(rec.DAY15).toString();	vo.sum += Number(rec.DAY15);
					vo.DAY16 = (rec.DAY16==0) ? "" : Number(rec.DAY16).toString();	vo.sum += Number(rec.DAY16);
					vo.DAY17 = (rec.DAY17==0) ? "" : Number(rec.DAY17).toString();	vo.sum += Number(rec.DAY17);
					vo.DAY18 = (rec.DAY18==0) ? "" : Number(rec.DAY18).toString();	vo.sum += Number(rec.DAY18);
					vo.DAY19 = (rec.DAY19==0) ? "" : Number(rec.DAY19).toString();	vo.sum += Number(rec.DAY19);
					vo.DAY20 = (rec.DAY20==0) ? "" : Number(rec.DAY20).toString();	vo.sum += Number(rec.DAY20);
					vo.DAY21 = (rec.DAY21==0) ? "" : Number(rec.DAY21).toString();	vo.sum += Number(rec.DAY21);
					vo.DAY22 = (rec.DAY22==0) ? "" : Number(rec.DAY22).toString();	vo.sum += Number(rec.DAY22);
					vo.DAY23 = (rec.DAY23==0) ? "" : Number(rec.DAY23).toString();	vo.sum += Number(rec.DAY23);
					vo.DAY24 = (rec.DAY24==0) ? "" : Number(rec.DAY24).toString();	vo.sum += Number(rec.DAY24);
					vo.DAY25 = (rec.DAY25==0) ? "" : Number(rec.DAY25).toString();	vo.sum += Number(rec.DAY25);
					vo.DAY26 = (rec.DAY26==0) ? "" : Number(rec.DAY26).toString();	vo.sum += Number(rec.DAY26);
					vo.DAY27 = (rec.DAY27==0) ? "" : Number(rec.DAY27).toString();	vo.sum += Number(rec.DAY27);
					vo.DAY28 = (rec.DAY28==0) ? "" : Number(rec.DAY28).toString();	vo.sum += Number(rec.DAY28);
					vo.DAY29 = (rec.DAY29==0) ? "" : Number(rec.DAY29).toString();	vo.sum += Number(rec.DAY29);
					vo.DAY30 = (rec.DAY30==0) ? "" : Number(rec.DAY30).toString();	vo.sum += Number(rec.DAY30);
					vo.DAY31 = (rec.DAY31==0) ? "" : Number(rec.DAY31).toString();	vo.sum += Number(rec.DAY31);
					return vo;
				}	
			}
			return null;
		}	
	}
}